﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class AuthorizeUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUnauthorizedUserList();
              
            }
        }

        protected void btnAuthorize_Click(object sender, EventArgs e)
        {
            AuthorizeUsers();
        }

        protected void grdAuthList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAuthList.PageIndex = e.NewPageIndex;
            GetUnauthorizedUserList();
        }

        protected void grdAuthList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        private void GetUnauthorizedUserList()
        {
            try
            {
                User currentUser = (User)Session["CurrentUserVO"];
                List<User> dtAuthList = (new UserEntity()).GetUnauthorizedUsers(currentUser.UserID, currentUser.UserType, currentUser.CustomerID);
                grdAuthList.DataSource = dtAuthList;
                grdAuthList.DataBind();
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
        }

        private void AuthorizeUsers()
        {
            User loggedInUser = ((User)Session["CurrentUserVO"]);
            List<User> lstUsers = new List<User>();

            try
            {
                int count = 0;
                foreach (System.Web.UI.WebControls.GridViewRow dgi in grdAuthList.Rows)
                {
                    CheckBox chkSelect = (CheckBox)dgi.Cells[0].FindControl("chkSelect");
                    if (chkSelect.Checked)
                    {
                        User oUser = new User()
                        {
                        CustomerID = loggedInUser.CustomerID,
                        UserID = dgi.Cells[1].Text.ToString(),
                        EmailAddress = dgi.Cells[4].Text.ToString(),
                        };
                        lstUsers.Add(oUser);
                    }
                }
                if (lstUsers.Count<User>() == 0)
                {
                    lblMessage.Text = "Please select a user to Authorize!";
                    btnClickPopup.Show();
                }
                else
                {
                    count = (new UserEntity()).AuthorizeUsers(loggedInUser.UserID, lstUsers);

                    //if count > 0 then success
                    if (count > 0)
                    {
                        lblMessage.Text = "Successfully Authorised (" + count + ") User!";
                        btnClickPopup.Show();
                    }
                    else
                    {
                        lblMessage.Text = "Failed to Authorised selected user(s)!";
                        btnClickPopup.Show();
                    }
                }

                //refresh list of unauthorized users
                GetUnauthorizedUserList();
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
            finally
            {
                lstUsers = null;
                loggedInUser = null;
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }
    }
}